package com.zhanbo.zhanbowymusic;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.mwx.baselib.base.BaseFragment;
import com.mwx.baselib.bean.LikeBean;
import com.mwx.baselib.bean.SongBean;
import com.mwx.baselib.constant.Constant;
import com.mwx.baselib.net.BaseResponses;
import com.mwx.baselib.net.DefaultObserver;
import com.mwx.baselib.net.NetworkManager;
import com.mwx.baselib.utils.GlideUtils;
import com.mwx.baselib.utils.MLog;
import com.zhanbo.zhanbowymusic.databinding.FragmentMainBinding;
import com.zhanbo.zhanbowymusic.main.HomeFragment;
import com.zhanbo.zhanbowymusic.main.bean.FeaturedPlaylistDetailsEBBean;
import com.zhanbo.zhanbowymusic.main.featured.FeaturedPlayListDetailsFragment;
import com.zhanbo.zhanbowymusic.music.MusicController;
import com.zhanbo.zhanbowymusic.music.OnEvent;
import com.zhanbo.zhanbowymusic.play.MusicPlayFragment;
import com.zhanbo.zhanbowymusic.play.MvPlayFragment;
import com.zhanbo.zhanbowymusic.play.bean.LikeEvBean;
import com.zhanbo.zhanbowymusic.play.bean.PlayMVBean;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 主页fragment
 */
public class MainFragment extends BaseFragment<FragmentMainBinding> implements View.OnClickListener {
    HomeFragment homeFragment = new HomeFragment();
    private MusicPlayFragment musicPlayFragment;
    MainActivity mainActivity ;
    private int currentPosition;
    private List<Integer> modeList = new ArrayList<>();
    private List<SongBean> songs;

    public static MainFragment newInstance(String param1, String param2) {
        MainFragment fragment = new MainFragment();

        return fragment;
    }



    @Override
    protected Object getLayout() {
        return R.layout.fragment_main;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        loadRootFragment(R.id.fl_main,homeFragment);
        mainActivity= (MainActivity) getActivity();
        initData();
        modeList.add(R.mipmap.shunxu);
        modeList.add(R.mipmap.suiji);
        modeList.add(R.mipmap.danqu);
        initListener();

    }
    @Subscribe
    public void getEvent(OnEvent event) {
        if (event.id== Constant.MUSIC_OVER||event.id==Constant.MUSIC_PAUSE){//歌曲播放结束或者暂停
            binding.ivPlay.setImageResource(R.mipmap.bofang_bar);
        }
        if (event.id==3) {
            if (binding.rlDockBar.getVisibility()==View.GONE){
                binding.rlDockBar.setVisibility(View.VISIBLE);
            }
            currentPosition = MusicController.getInstance().getCurrentPosition();
          songs = MusicController.getInstance().getSheetData();
            if (currentPosition < songs.size()) {
                binding.tvName.setText(songs.get(currentPosition).getName());
                binding.tvArtistName.setText("歌手：" + songs.get(currentPosition).getArtistName());
                GlideUtils.loadImageRounded10(_mActivity, songs.get(currentPosition).getCoverImg(), binding.ivHead);
            }
            if (songs.get(currentPosition).getIsLike()!=1){
                binding.ivLike.setImageResource(R.mipmap.like_no);
            }else {
                binding.ivLike.setImageResource(R.mipmap.like);
            }
        }
        if (event.id==4){
            binding.ivPlay.setImageResource(R.mipmap.zanting_bar);
        }
        if (event.id==Constant.MUSIC_MODE){
            modeNum=MusicController.musicController.getchangeMode();
            binding.ivMode.setImageResource(modeList.get(modeNum));
        }


    }
    @Subscribe
    public void isLike(LikeEvBean likeEvBean){
        if (likeEvBean.getId()==songs.get(currentPosition).getId()){
            if (likeEvBean.getIsLike()!=1){
                binding.ivLike.setImageResource(R.mipmap.like_no);

            }else {
                binding.ivLike.setImageResource(R.mipmap.like);
            }
        }
    }
    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initListener() {
        binding.rlDockBar.setOnClickListener(this);
        binding.ivPlayList.setOnClickListener(this);
        binding.ivPlay.setOnClickListener(this);
        binding.ivLastOne.setOnClickListener(this);
        binding.ivNextOne.setOnClickListener(this);
        binding.ivMode.setOnClickListener(this);
        binding.ivLike.setOnClickListener(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void openMvpage(PlayMVBean playMVBean){

        start(MvPlayFragment.newInstance(playMVBean.getMvBean()),SINGLETASK);
    }
    private int modeNum=0;
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.rl_dock_bar:
//                if (musicPlayFragment==null){
//                    musicPlayFragment  = MusicPlayFragment.newInstance();
//                }
                start(MusicPlayFragment.newInstance());

                break;
            case R.id.iv_play_list:
                mainActivity.showPlayListDialog();
                break;
            case R.id.iv_last_one:
                MusicController.getInstance().lastOne();
                break;
            case R.id.iv_next_one:
                MusicController.getInstance().nextOne();
                break;
            case R.id.iv_play:
                if (MusicController.musicController.isPlaying){
                    MusicController.getInstance().pause();
                }else {
                    MusicController.getInstance().play();

                }
                break;
            case R.id.iv_mode:
                modeNum++;
                if (modeNum>modeList.size()-1){
                    modeNum=0;
                }
                binding.ivMode.setImageResource(modeList.get(modeNum));
                MusicController.getInstance().changeMode(modeNum);
                break;
            case R.id.iv_like:
                like();
                break;
        }
    }
    private void like(){
        Map<String,String> map = new HashMap<>();
        map.put("targetId",songs.get(currentPosition).getId()+"");
        NetworkManager.getInstance().getApiService().songLike(map)
                .subscribeOn(Schedulers.io())
                .compose(this.bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new DefaultObserver<BaseResponses<LikeBean>>(_mActivity,false) {
                    @Override
                    public void onSuccess(BaseResponses<LikeBean> listBaseResponses) {

                    }

                    @Override
                    public void onNext(BaseResponses<LikeBean> likeBeanBaseResponses) {
                        if (likeBeanBaseResponses.getCode()==200) {

                            if (likeBeanBaseResponses.getData()==null||"".equals(likeBeanBaseResponses.getData())){
                                binding.ivLike.setImageResource(R.mipmap.like_no);
                                songs.get(currentPosition).setIsLike(0);
                            }else {
                                binding.ivLike.setImageResource(R.mipmap.like);
                                songs.get(currentPosition).setIsLike(1);
                            }
                        }

                    }
                });
    }
    @Override
    public void onDestroy() {

        EventBus.getDefault().unregister(this);

        super.onDestroy();
    }
}